const router = require('koa-router')()
import axios from 'axios'
import user from '../db/models/user'
import Passport from '../until/passport'
import moment from 'moment'
import multer from '@koa/multer'
import fs from 'fs'
router.prefix('/users')
router.post('/signin', async (ctx,next) => {
    return Passport.authenticate('local',function (err,user,info,status) { // 使用本地策略 进行登录校验
          if (err) {
            ctx.body={
              code:-1,
              msg: err
            }
          }else {
            if (user) {
              let createTime = moment(user.createTime).format('YYYY-MM-DD HH:mm:ss')
              let lastloginTime = moment(user.lastloginTime).format('YYYY-MM-DD HH:mm:ss')
              let newObj = Object.assign({},user['_doc'])
              newObj.createTime = createTime
              newObj.lastloginTime = lastloginTime
              ctx.body={
                code:0,
                msg: '登录成功',
                data: newObj
              }
              return ctx.login(user)
            }else {
              ctx.body={
                code:1,
                msg:info
              }
            }
          }
    })(ctx,next)
  })
module.exports = router
